import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';


class StatelessWidgetDemo  extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel(
      model: CounterModel(),
      child:  Scaffold(
        appBar: AppBar(
          title:Text('StatefulWidgetDemo'),
          elevation: 0.0,
        ),
        body: Counter(),
        floatingActionButton: ScopedModelDescendant<CounterModel>(
          rebuildOnChange: false,
          builder: (context,_,modle) => FloatingActionButton(onPressed:modle.increaseCount,
          child: Icon(Icons.add)
          ),
        ),
    ),
    );
  }
}

// class StatefulWidgetDemo extends StatefulWidget {
//   @override
//   _StatefulWidgetDemoState createState() => _StatefulWidgetDemoState();
// }

// class _StatefulWidgetDemoState extends State<StatefulWidgetDemo> {
//   int _count = 0;
//   @override
//   Widget build(BuildContext context) {
//      return CounterProvider(
//        count:_count,
//        child:  Scaffold(
//         appBar: AppBar(
//           title:Text('StatefulWidgetDemo'),
//           elevation: 0.0,
//         ),
//         body: Counter(),
//         floatingActionButton: FloatingActionButton(
//           onPressed:(){
//             setState(() {
//               _count += 1;
//             });
//           },
//           child: Icon(Icons.add),
//         ),
//     ),
//      );
// }
// }

// class Counter extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//       final  int _count = CounterProvider.of(context).count;
//     return Container(
//          //padding: EdgeInsets.all(10),
//          child: Column(
//          mainAxisAlignment: MainAxisAlignment.center,
//           children: <Widget>[ 
//             Center(
//               child: Text('$_count',style: TextStyle(fontSize: 20),)
//             ),
//           ],
//          ),
//         );
//   }
// }

class Counter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
   return ScopedModelDescendant<CounterModel>(
     builder: (context,_,model) => Center(
              child: Text('${model.count}',style: TextStyle(fontSize: 20))
            ),
     );
  }

}

class CounterProvider extends InheritedWidget{
     final int count;
     final Widget child;
     CounterProvider({
         this.count,
         this.child
     }):super(child:child);

   static of(BuildContext context) =>
   context.inheritFromWidgetOfExactType(CounterProvider);

   @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return true;
  }
}

class CounterModel extends Model{
     int _count = 0;
     int get count => _count;
     void increaseCount(){
       _count += 1;
       notifyListeners();
     }
}
